﻿using FreedomX2.Core.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FreedomX2.Core.Log;
namespace FreedomX2.Core.Transport.SocketProvider
{
    public class SocketConvert
    {

        public static byte[] convertToBuffer(BlocksThreadSingle v)
        {

            byte[] bytesReturn = new byte[v.BytesList.Count + SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength + SocketBlocks.FootSizeLength + SocketBlocks.FootCustomLength];
            try
            {
                lock (bytesReturn)
                {
                    byte[] bytesLogo = new byte[SocketBlocks.HeadLogoLength];
                    byte[] bytesID = new byte[SocketBlocks.HeadIDLength];
                    byte[] bytesSize = new byte[SocketBlocks.FootSizeLength];
                    byte[] bytesCustom = new byte[SocketBlocks.FootCustomLength];
                    byte[] bytesContent = Convertor.ConvertObjectsToBytes(v.BytesList.ToArray());
                    Array.Copy(Convertor.ConvertStringToBytes(v.ID.ToString()), bytesLogo, Convertor.ConvertStringToBytes(v.ID.ToString()).Length);
                    Array.Copy(Convertor.ConvertStringToBytes(v.SerialID.ToString()), bytesID, Convertor.ConvertStringToBytes(v.SerialID.ToString()).Length);
                    Array.Copy(Convertor.ConvertStringToBytes(v.Size.ToString()), bytesSize, Convertor.ConvertStringToBytes(v.Size.ToString()).Length);
                    Array.Copy(Convertor.ConvertStringToBytes(v.Custom.ToString()), bytesCustom, Convertor.ConvertStringToBytes(v.Custom.ToString()).Length);
                    Array.Copy(bytesLogo, bytesReturn, SocketBlocks.HeadLogoLength);
                    Array.Copy(bytesID, 0, bytesReturn, SocketBlocks.HeadLogoLength, SocketBlocks.HeadIDLength);
                    Array.Copy(bytesContent, 0, bytesReturn, SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength, bytesContent.Length);
                    Array.Copy(bytesSize, 0, bytesReturn, SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength + v.BytesList.Count, SocketBlocks.FootSizeLength);
                    Array.Copy(bytesCustom, 0, bytesReturn, SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength + SocketBlocks.FootSizeLength + v.BytesList.Count, SocketBlocks.FootCustomLength);
                }
            }
            catch (Exception ex)
            {
                ex.e();
            }
            return bytesReturn;

        }



        public static BlocksThreadSingle ConvertToBTS(byte[] bytes)
        {
            var v = new BlocksThreadSingle();

            try
            {
                lock (bytes)
                {
                    byte[] bytesLogo = new byte[SocketBlocks.HeadLogoLength];
                    byte[] bytesID = new byte[SocketBlocks.HeadIDLength];
                    byte[] bytesSize = new byte[SocketBlocks.FootSizeLength];
                    byte[] bytesCustom = new byte[SocketBlocks.FootCustomLength];
                    byte[] bytesContent = new byte[bytes.Length - (SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength + SocketBlocks.FootSizeLength + SocketBlocks.FootCustomLength)];
                    Array.Copy(bytes, bytesLogo, SocketBlocks.HeadLogoLength);
                    Array.Copy(bytes, SocketBlocks.HeadLogoLength, bytesID, 0, SocketBlocks.HeadIDLength);
                    Array.Copy(bytes, bytes.Length - (SocketBlocks.FootSizeLength + SocketBlocks.FootCustomLength), bytesSize, 0, SocketBlocks.FootSizeLength);
                    Array.Copy(bytes, bytes.Length - SocketBlocks.FootCustomLength, bytesCustom, 0, SocketBlocks.FootCustomLength);
                    Array.Copy(bytes, SocketBlocks.HeadLogoLength + SocketBlocks.HeadIDLength, bytesContent, 0, bytesContent.Length);
                    string strLogo = Convertor.ConvertBytesToString(bytesLogo);
                    string strSerialID = Convertor.ConvertBytesToString(bytesID);
                    string strSize = Convertor.ConvertBytesToString(bytesSize);
                    string strCustom = Convertor.ConvertBytesToString(bytesCustom);
                    v.ID = Convert.ToInt32(strLogo);
                    v.SerialID = Convert.ToInt32(strSerialID);
                    v.Size = Convert.ToInt32(strSize);
                    v.Custom = Convert.ToInt32(strCustom);
                    v.BytesList.AddRange(bytesContent);
                }
            }
            catch (Exception ex)
            {
                ex.e();
            }

            return v;

        }


    }
}
